package com.tian.clothing.base;

import com.jd.open.sdk.android.JdAndroidClient;
import com.tian.clothing.R;
import com.tian.clothing.constant.GlobalConstant;
import com.tian.clothing.global.ClothingApplication;
import com.tian.clothing.network.asynctask.AsyncTaskListener;
import com.tian.clothing.util.DialogUtil;
import com.tian.clothing.widget.TitleBar;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;

/**
 * @ClassName: BaseActivity
 * @Description: 所有类的基类
 * @author: dreamfutureba@gmail.com
 * @date: 2013年11月20日 下午4:54:59
 */
public abstract class BaseActivity extends FragmentActivity{
	private static final String TAG = BaseActivity.class.getSimpleName();

	protected ClothingApplication mGlobal = ClothingApplication.getApp();

	// 通过关键字查询商品
    protected static final int SEARCH_BY_KEYWORD = 1;
    // 通过类目ID查询商品
    protected static final int SEARCH_BY_CATEGORYID = 2;
    // 数据加载提示
    protected static final String LOADING = "数据加载中...";
    protected JdAndroidClient client = JdAndroidClient.getInstance();
	
	
	protected Context mContext;// 上下文
	protected TitleBar mTitleBar;
	protected LinearLayout mContentLayout;

	protected DialogUtil dialogUtil;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		registerFinishBroadcast();
		
		if (needObserver()) {
			mGlobal.addActivity(this);
		}
		
		initBaseData();
		initTitleBar();
		installViews();
		registerEvents();
	}

	@Override
	protected void onDestroy() {
		if (needObserver()) {
			mGlobal.removeActivity(this);
		}
		try {
			unregisterReceiver(mFinishBroadcast);
		} catch (Exception e) {
			e.getStackTrace();
		}

		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	// 注册广播
	private void registerFinishBroadcast() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(GlobalConstant.CLOSE_ALL_ACTIVITY);
		registerReceiver(mFinishBroadcast, filter);
	}

	/**
	 * @Fields mFinishBroadcast : 使用广播关闭当前的activity
	 */
	protected BroadcastReceiver mFinishBroadcast = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				unregisterReceiver(this);
			} catch (Exception e) {
			}
			((Activity) context).finish();
		}
	};

	@Override
	public void setContentView(int layoutResID) {
		LayoutInflater inflater = LayoutInflater.from(BaseActivity.this);
		inflater.inflate(layoutResID, mContentLayout);
	}

	@Override
	public void setContentView(View view) {
		super.setContentView(view);
	}

	private void initBaseData() {
		mContext = this;
		super.setContentView(R.layout.activity_base);
		mTitleBar = (TitleBar) this.findViewById(R.id.base_titlebar);
		mContentLayout = (LinearLayout) this.findViewById(R.id.base_content);
		dialogUtil=new DialogUtil(mContext);
	}

	/**
	 * TitleBar图标或title的更换,显示等操作。
	 */
	protected void initTitleBar() {
		// 设置左边的按钮的事件
		if (null != mTitleBar && null != mTitleBar.titlebar_btn_left) {
			mTitleBar.titlebar_btn_left.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}
	}

	protected boolean needObserver() {
		return true;
	}

	/**
	 * 加载布局文件，初始化对应的view对象。
	 */
	protected abstract void installViews();

	/**
	 * Activity中所有View的绑定事件，都放置到该类中。
	 */
	protected abstract void registerEvents();
	
	/**
	 * 异步操作显示Dialog
	 */
	public void showProgressDialog(String msg) {

		dialogUtil.showProgressDialog(msg);
	}
	/**
	 * 异步操作关闭Dialog
	 */
	public void closeProgressDialog() {
		dialogUtil.closeProgressgDialog();
	}
}
